home *** CD-ROM | disk | FTP | other *** search
/ 5 Star Games: DOS Edition 2 / 5 Star Games - DOS Edition (1995)(Ready to Run).iso / dbc / db_str.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-07  |  7.5 KB  |  297 lines

  1. /****************************************************************************/
  2. /*                        DATABOSS MODULE: DB_STR.C                         */
  3. /****************************************************************************/
  4.  
  5. #include "db_lsc.h"
  6.  
  7. #include <stdarg.h>
  8. #include <stddef.h>
  9. #include <string.h>
  10. #include "db_types.h"
  11. #include "db_str.h"
  12.  
  13. /****************************  IMPLEMENTATION  ******************************/
  14.  
  15. /* new for 3.6 works exactly the same as sprintf see your compiler documentation
  16.     except returns the string passed in. so it can be used in DataBoss Expressions
  17.  
  18.    Chris
  19. */
  20.  
  21.  
  22.  
  23. char* DB_Sprintf( char* pachBuffer, char* pchFormat,...)
  24. {
  25.     va_list ap;
  26.     va_start(ap,pchFormat);
  27.     vsprintf(pachBuffer,pchFormat,ap);
  28.     return ( pachBuffer );
  29. }
  30.  
  31.  
  32. strptr strcopy(string sout, string s, int from, int len)
  33. {
  34.     int slen;
  35.  
  36.     slen = strlen(s);
  37.     if (from < slen) {
  38.         if ((from + len) > slen) len = slen - from;
  39.         memmove(sout,&s[from],len);
  40.         sout[len] = '\0';
  41.     }
  42.     else
  43.         sout[0] = '\0';
  44.     return (sout);
  45. }
  46.  
  47. strptr strconcat(string sout, ...)
  48. {
  49.     va_list   ap;
  50.     string s;
  51.     strptr dest,sarg;
  52.  
  53.     dest = s;
  54.     va_start(ap,sout);
  55.     while ((sarg = va_arg(ap,strptr)) != NULL) dest = stpcpy(dest,sarg);
  56.     va_end(ap);
  57.     *dest = '\0';
  58.     return (strcpy(sout,s));
  59. }
  60.  
  61. strptr strdelete(string s, int from, int len)
  62. {
  63.     int slen;
  64.  
  65.     slen = strlen(s);
  66.     if ((len != 0) && (from < slen))
  67.         strcpy(&s[from],&s[((from + len) < slen) ? from + len : slen]);
  68.     return (s);
  69. }
  70.  
  71. strptr strinsert(string sins, string s, int pos)
  72. {
  73.     int slen,ilen;
  74.  
  75.     slen = strlen(s);
  76.     ilen = strlen(sins);
  77.     if (ilen) {
  78.         if (pos < slen) {
  79.             memmove(&s[pos+ilen],&s[pos],slen-pos+1);
  80.             memmove(&s[pos],sins,ilen);
  81.         }
  82.         else
  83.             strcpy(&s[slen],sins);
  84.     }
  85.     return (s);
  86. }
  87.  
  88. int strposstr(string chkfor, string s)
  89. {
  90.     uchar huge *hp;
  91.     uchar huge *hs;
  92.  
  93.    hs = s;
  94.     hp = strstr(s, chkfor);
  95.     return ((hp == NULL) ? -1 : (int) (hp - hs));
  96. }
  97.  
  98. int strposch(uchar c, string s)
  99. {
  100.     uchar huge *hp;
  101.     uchar huge *hs;
  102.  
  103.     hs = s;
  104.     hp = strchr(s,c);
  105.     return ((hp == NULL) ? -1 : (int) (hp - hs));
  106. }
  107.  
  108. strptr chstr(string sout, byte c)
  109. {
  110.     *((word *) sout) = (word) c;
  111.     return (sout);
  112. }
  113.  
  114. strptr strchcat(strptr s, uchar ch)
  115. {
  116.     strptr tp;
  117.  
  118.     tp = strchr(s,0);
  119.     *tp++ = ch;
  120.     *tp = 0;
  121.     return(s);
  122. }
  123.  
  124. strptr pastocstr(strptr ps)
  125. {
  126.     byte b;
  127.  
  128.     b = *ps;
  129.     memmove(ps,ps+1,b);
  130.     ps[b] = '\0';
  131.     return(ps);
  132. }
  133.  
  134.  
  135. /*
  136. ****************************************************************************
  137. **  Function   : mid
  138. **
  139. **  Arguments  : (char *) - string to store result in
  140. **               (char *) - string to extract from
  141. **               (int)    - start position in source string
  142. **               (int)    - length in bytes
  143. **  Returns    : (char *) - a pointer to the destination string
  144. **
  145. **  This function attempts to extract a portion of the source string and
  146. **  place that portion in the destination string.
  147. **
  148. **    Date      Version      Who      Description
  149. **  --------------------------------------------------------------------
  150. **  01/06/90      1.0        SN       Original Version
  151. **  10/07/91      1.1        SN       Used to return nothing, now returns
  152. **                                    pointer to destination
  153. **
  154. ****************************************************************************
  155. */
  156.  
  157.  
  158. char * mid(char *destination, char *source, int start, int length)
  159. {
  160.   char *startOfDestination;
  161.  
  162.   source             = source + start;
  163.   startOfDestination = destination;
  164.   while ((*destination++ = *source++) != '\0' && (--length)) continue;
  165.  
  166.   if (!length) *destination = '\0';
  167.   return(startOfDestination);
  168. }
  169.  
  170.  
  171. /*
  172. ****************************************************************************
  173. **  Function   : strgt
  174. **
  175. **  Arguments  : (char *) - string1 is the string to test on
  176. **               (char *) - string2 is the string to compare against
  177. **  Returns    : (int)    - (0-False, 1-True)
  178. **
  179. **  This function attempts to compare string1 against string2 to see if
  180. **  string1 is greater than string2.
  181. **
  182. **    Date      Version      Who      Description
  183. **  --------------------------------------------------------------------
  184. **  07/10/91      1.0        SN       First version
  185. **
  186. ****************************************************************************
  187. */
  188.  
  189.  
  190. int strgt(char *source1, char *source2)
  191. {
  192.   return(strcmp(source1, source2) > 0);
  193. }
  194.  
  195.  
  196. /*
  197. ****************************************************************************
  198. **  Function   : strlt
  199. **
  200. **  Arguments  : (char *) - string1 is the string to test on
  201. **               (char *) - string2 is the string to compare against
  202. **  Returns    : (int)    - (0-False, 1-True)
  203. **
  204. **  This function attempts to compare string1 against string2 to see if
  205. **  string1 is less than string2.
  206. **
  207. **    Date      Version      Who      Description
  208. **  --------------------------------------------------------------------
  209. **  07/10/91      1.0        SN       First version
  210. **
  211. ****************************************************************************
  212. */
  213.  
  214.  
  215. int strlt(char *source1, char *source2)
  216. {
  217.   return(strcmp(source1, source2) < 0);
  218. }
  219.  
  220.  
  221. /*
  222. ****************************************************************************
  223. **  Function   : strgte
  224. **
  225. **  Arguments  : (char *) - string1 is the string to test on
  226. **               (char *) - string2 is the string to compare against
  227. **  Returns    : (int)    - (0-False, 1-True)
  228. **
  229. **  This function attempts to compare string1 against string2 to see if
  230. **  string1 is greater than or equal to string2.
  231. **
  232. **    Date      Version      Who      Description
  233. **  --------------------------------------------------------------------
  234. **  07/10/91      1.0        SN       First version
  235. **
  236. ****************************************************************************
  237. */
  238.  
  239.  
  240. int strgte(char *source1, char *source2)
  241. {
  242.   return(strcmp(source1, source2) >= 0);
  243. }
  244.  
  245.  
  246. /*
  247. ****************************************************************************
  248. **  Function   : strlte
  249. **
  250. **  Arguments  : (char *) - string1 is the string to test on
  251. **               (char *) - string2 is the string to compare against
  252. **  Returns    : (int)    - (0-False, 1-True)
  253. **
  254. **  This function attempts to compare string1 against string2 to see if
  255. **  string1 is less than or equal to string2.
  256. **
  257. **    Date      Version      Who      Description
  258. **  --------------------------------------------------------------------
  259. **  07/10/91      1.0        SN       First version
  260. **
  261. ****************************************************************************
  262. */
  263.  
  264.  
  265. int strlte(char *source1, char *source2)
  266. {
  267.   return(strcmp(source1, source2) <= 0);
  268. }
  269.  
  270.  
  271. /*
  272. ****************************************************************************
  273. **  Function   : strne
  274. **
  275. **  Arguments  : (char *) - string1 is the string to test on
  276. **               (char *) - string2 is the string to compare against
  277. **  Returns    : (int)    - (0-False, 1-True)
  278. **
  279. **  This function attempts to compare string1 against string2 to see if
  280. **  string1 is not equal to string2.
  281. **
  282. **    Date      Version      Who      Description
  283. **  --------------------------------------------------------------------
  284. **  07/10/91      1.0        SN       First version
  285. **
  286. ****************************************************************************
  287. */
  288.  
  289.  
  290. int strne(char *source1, char *source2)
  291. {
  292.   return(strcmp(source1, source2) != 0);
  293. }
  294.  
  295.  
  296. /****************************  END OF DB_STR.C  *****************************/
  297.